package com.person.monitor;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.camera2.params.BlackLevelPattern;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

import java.util.List;

import static android.content.Context.WINDOW_SERVICE;

public class AutoLightProcess extends BaseProcess{
    float lastBrightness;
    Handler handler = new Handler();

    View floatView;

    int BrightnessLevels[];
    int BackLightValues[];

    WindowManager windowManager;

    public AutoLightProcess(Context context,String name) {
        super(context,name);

        BrightnessLevels = context.getResources().getIntArray(R.array.config_autoBrightnessLevels);
        BackLightValues = context.getResources().getIntArray(R.array.config_autoBrightnessLcdBacklightValues);

        windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
    }

    @Override
    public void processMonitor(List<MonitorInfo> list) {
        if (list.size() > 3){
            MonitorInfo now = list.get(0);
            MonitorInfo last = list.get(1);
            MonitorInfo early = list.get(2);

            float lux = (now.lightLux + last.lightLux + early.lightLux)/3;

            handler.post(()->setBrightness(getBrightness(lux)));
        }
    }

    @Override
    public void setProcessEnable(boolean enable) {
        if(getProcessEnable() != enable) {
            super.setProcessEnable(enable);

            if (enable) {

            } else {
                if(floatView != null) {
                    windowManager.removeView(floatView);
                    floatView = null;
                }
            }
        }
    }

    private void setBrightness(float brightness){
        if(brightness == lastBrightness){
            return;
        }
        // 设置LayoutParam
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.width = 1;
        layoutParams.height = 1;
        layoutParams.x = 1;
        layoutParams.y = 1;
        layoutParams.screenBrightness = brightness;

        if(floatView == null) {
            floatView = new View(context);
            floatView.setBackgroundColor(Color.TRANSPARENT);
            windowManager.addView(floatView, layoutParams);
        }else {
            windowManager.updateViewLayout(floatView, layoutParams);
        }

        lastBrightness = brightness;
    }

    private float getBrightness(float lux){
        lux = lux*2f;

        float brightness;

        int index = 0;

        for (int i=0;i<BrightnessLevels.length;i++){
            if(lux > BrightnessLevels[i]){
                index = i;
            }
        }

        int light = BackLightValues[index];

        brightness = light/255f;

        if(brightness > 1f) brightness = 1f;

        //LogcatHelper.getInstance().printLog("index="+index+",light="+light+",bright="+brightness);

        return brightness;
    }
}
